# -*- coding: UTF-8 -*- 
import httplib,urllib
import time,json

def gettoken(client_id,client_secret):
    url='http://api.before.com/api-cloud-platform/oauth2/token' #api.before.com需要根据具体访问域名修改
    post_data="grant_type=client_credentials&client_id=%s&client_secret=%s"%(client_id,client_secret)
    req = urllib.urlopen(url, post_data)
    responsecontent = req.read()
    responsedict=json.loads(responsecontent)
    token=responsedict["access_token"]
    return token

def apiget(tokent,sdate,edate):
    url = "http://webapi.cninfo.com.cn/api/info/p_info3015?access_token=%s&sdate=%s&edate=%s" #apitest2.com需要根据具体访问域名修改
    conn = httplib.HTTPConnection("webapi.cninfo.com.cn")
    conn.request(method="GET",url=url%(scode,tokent))
    response = conn.getresponse()
    rescontent= response.read()
    responsedict=json.loads(rescontent)
    resultcode=responsedict["resultcode"]
    print (responsedict["resultmsg"],responsedict["resultcode"])
    if(responsedict["resultmsg"]=="success" and len(responsedict["records"])>=1):
        print( responsedict["records"] ) #接收到的具体数据内容
    else:
        print ('no data')
    return resultcode


if __name__=="__main__":
    client_id,client_secret="100001","8a6a4b0595ca4d248966b76a080a0c9d" #client_id,client_secret通过我的凭证获取
    token=gettoken(client_id,client_secret)
    apiget(token,'2017-12-11','2017-12-11') 
   